﻿$(function () {
    $("#go").click(function () {
        $("#tb_result").html("");

        //1.有效性检查
        var province = $("#province").val().replace(/\s+/g, "");
        var city = $("#city").val().replace(/\s+/g, "");
        var district = $("#district").val().replace(/\s+/g, "");
        var town = $("#town").val().replace(/\s+/g, "");
        var village = $("#village").val().replace(/\s+/g, "");

        //$(this).val("查询中……");
        //$(this).attr("disabled", "disabled");
        disableBtn();

        //2.ajax提交
        $.ajax({
            url: "/ashx/cunSearchHandler.ashx",
            async: false,
            type: "post",
            dataType: "json",
            data: {
                province: province,
                city: city,
                district: district,
                town: town,
                village: village
            },
            success: function (data) {
                if (data.Success) {
                    iniTable($.parseJSON(data.Data));
                } else {
                    alert(data.Message);
                }
                enableBtn();
            }
        });
    });

    //文本框按回车触发按钮
    $("input").keyup(function (event) {
        if (event.keyCode == 13) {
            $("#go").click();
        }
    });
});

function iniTable(jsonstr) {
    $.each(jsonstr, function (idx, v) {
        var $tr = $("<tr></tr>");
        $tr.append("<td>" + v.province + "</td>");
        $tr.append("<td>" + v.city + "</td>");
        $tr.append("<td>" + v.district + "</td>");
        $tr.append("<td>" + v.town + "</td>");
        $tr.append("<td>" + v.village + "</td>");
        $tr.append("<td>" + v.name + "</td>");

        var m = "";
        //console.log(v.Memo);
        if (v.Memo) {
            var z = $.parseJSON(v.Memo);
            m = JSON.stringify(z["person"])
            $tr.append("<td data-toggle='tooltip' data-placement='right' title='" + m + "'><span style='color:green;'>" + v.tel + "</span></td>");
        } else {
            $tr.append("<td>" + v.tel + "</td>");
        }
        $("#tb_result").append($tr);

    });
}

function disableBtn() {
    $("#go").text("正在查询……");
    $("#go").attr("disabled", "disabled");
    $("#go").addClass('disabled');
}

function enableBtn() {
    $("#go").text("开始查询");
    $("#go").attr("disabled", false);
    $("#go").removeClass('disabled');
}